<?php
require_once 'mysql.inc.php';

$pinfo = $_SESSION['pinfo'];

if (isset($_SESSION['pinfo'])) {
   // session is personalised, we see actual person's events plus invitations 
    $personal_filter = sprintf(" WHERE (e.person = '%s' "
            ." OR e.id IN (SELECT  event FROM `GuestsInSimpleEvents` g WHERE g.person = '%s'))"
            ." AND ", 
            $pinfo['id'], $pinfo['id']);} 
else {
    // person not set
    if (AmIAdmin(2)) $personal_filter = " WHERE  "; // superadmin: we see everything but no invitations 
    else $personal_filter = " WHERE (e.type in (1, 2)) AND " ;// admin(1): type 1,2 only // schůzky jiřka nevidí
};

$sql = sprintf("SELECT * FROM `SimpleEvents` e "
        . $personal_filter 
        . " (MONTH(e.DateBegin)=%d OR MONTH(e.DateEnd)=%d) AND (YEAR(e.DateBegin)=%d OR YEAR(e.DateEnd)=%d)",
        $_SESSION['view']['current']['M'], $_SESSION['view']['current']['M'], $_SESSION['view']['current']['Y'], $_SESSION['view']['current']['Y']);
//echo $sql;
$udalosti = mysql_query($sql) or die(mysql_error());

$PopisTypuUdalosti = array(0 => "?", 1 => "D", 2 => "C", 3=> "A", 4=> "P");
$PopisTypuUdalostiDetail = array(0 => "?", 1 => "Dovolená", 2 => "Cesta", 
                                 3 => "Aktivita (vykazuje se, např. schůzka)",
                                 4 => "Pasivita (NEvykazuje se, např, účast na semináři)");


function Granty($event_id) {
    $gs = SQLGrantyEvent($event_id);
    $out = "";
    foreach ($gs as $g) { //print_r($row);
        $out = $out . '<tiny title="' . $g['Name'] . '">  ' . $g['Short'] . " </tiny>";
    }
    return($out);
}
function CurTimeSel($i) {
    if ($i == $_SESSION['view']['current']['M'] or $i == $_SESSION['view']['current']['Y'])
        echo 'selected = "selected"';
}

//print_r($_SESSION['view']);
?>

<table border="0">
    <tr><td>
            <?php //GenerateChangePersonForm("vogoni.php"); ?>
            <form  name="form_action_VyberObdobi"  action="vogoni.php" method="POST">
                <select name="form_action_VyberMesice" onChange="document.form_action_VyberObdobi.submit();">
                    <option value="1" <?php CurTimeSel(1) ?> >Leden</option>
                    <option value="2" <?php CurTimeSel(2) ?> >Únor</option>
                    <option value="3" <?php CurTimeSel(3) ?> >Březen</option>
                    <option value="4" <?php CurTimeSel(4) ?> >Duben</option>
                    <option value="5" <?php CurTimeSel(5) ?> >Květen</option>
                    <option value="6" <?php CurTimeSel(6) ?> >Červen</option>
                    <option value="7" <?php CurTimeSel(7) ?> >Červenec</option>
                    <option value="8" <?php CurTimeSel(8) ?> >Srpen</option>
                    <option value="9" <?php CurTimeSel(9) ?> >Září</option>
                    <option value="10" <?php CurTimeSel(10) ?> >Říjen</option>
                    <option value="11" <?php CurTimeSel(11) ?> >Listopad</option>
                    <option value="12" <?php CurTimeSel(12) ?> >Prosinec</option>
                </select>
                <select name="form_action_VyberRoku"  onChange="document.form_action_VyberObdobi.submit();">
                    <option <?php CurTimeSel(2012) ?> >2012</option>
                    <option <?php CurTimeSel(2013) ?> >2013</option>
                    <option <?php CurTimeSel(2014) ?> >2014</option>
                </select>
            Ve zvoleném období je vidět <?php echo mysql_num_rows($udalosti); ?> událostí.
            </form>
        </td>
    </tr>
    <tr><td>          
            <table class="EventListTableStyle" border="1" cellpadding="3" cellspacing="0" rules="rows">        
                <thead class="EventListRowOdd">
                    <tr>
                        <?php if (AmIAdmin()) echo'<th>Kdo</th>' ?>
                        <th>Druh</th>
                        <th>Od</th>
                        <th>Do</th>
                        <th>Cíl</th>                
                        <th>Granty</th>
                        <th colspan="3">Akce</th>
                        <th>Popis</th>
                        <th>Poznámka</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $i = 0;
                    while ($row = mysql_fetch_assoc($udalosti)) {
                        /*
                        $akceTisk = array(
                            1 => '<form name="UdalostTisk" method="POST" action="TiskUdalosti.php" target="_blank">'
                            . '<input type="hidden" name="TypDat" value="UdalostTisk">'
                            . sprintf('<input type="hidden" name="tisknout_udalost_id" value="%s"/>', $row['id'])
                            . '<input type="image" value="Tisk" class="EventListActionStyle" src="img/print.ico" alt="Tisk" title="Tisk"'
                            //    .'onSubmit="window.open (\'Tisk dovolené\',\'Tisk dovolené\',scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no")'
                            . '/>'
                            . '</form>',
                            2 => '<form name="UdalostTisk" method="POST" action="TiskUdalosti.php" target="_blank">'
                            . '<input type="hidden" name="TypDat" value="UdalostTisk">'
                            . sprintf('<input type="hidden" name="tisknout_udalost_id" value="%s"/>', $row['id'])
                            . '<input type="image" value="Tisk" class="EventListActionStyle" src="img/print.ico" alt="Tisk" title="Tisk"'
                            //    .'onSubmit="window.open (\'Tisk dovolené\',\'Tisk dovolené\',scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no")'
                            . '/>'
                            . '</form>'
                        );
                        */
                        $odkazTisk = '<a href="'.GetPdfUrl($row['id']).'" '
                                .' target="_blank" alt="pdf"> '
                                .' <IMG class="EventListActionStyle" src="img/print.ico" alt="pdf"></a>';
                         /*
                            $odkazTisk = $odkazTisk
                            . '<a href="'.PdfGeneratorQueryUrl($row['id']).'" target="_blank" alt="Generuj znovu pdf">G</a>';
                             *
                             */

                        $akceTisk = array(
                            1 => $odkazTisk,
                            2 => $odkazTisk
                        );                        

                        if ($row['id'] == $_SESSION['view']['highlight_id']) {
                            echo '<tr class="EventListRowHigh">';
                            $i++;
                        } elseif (($i++ % 2) == 0)
                            echo '<tr class="EventListRowEven">';
                        else
                            echo '<tr class="EventListRowOdd">';

                        if (AmIAdmin()) {
                            $p = $_SESSION['Global']['People'][$row['person']];
                            printf('<td><div title="%s">%s</div></td>', PersonName($p), $p['Login']);
                        }
                        
                        if ($row['person']==$pinfo['id'] || (! isset($_SESSION['pinfo']))) { // jsem vlastnikem akce / admin
                          printf('<td title="%s">%s</td>', $PopisTypuUdalostiDetail[$row['type']],$PopisTypuUdalosti[$row['type']]);
                          $akce0 = '<form name="UdalostSmazat" method="POST" action="vogoni.php">'
                                . '<input type="hidden" name="TypDat" value="UdalostSmazat">'
                                . sprintf('<input type="hidden" name="smazat_udalost_id" value="%s"/>', $row['id'])
                                . '<input type="image" value="Smazat" class="EventListActionStyle" src="img/delete.ico" alt="Smazat" title="Smazat"/>'
                                . '</form>';
                         
                         if ($row['type']==1 || $row['type']==2) {
                             $akce1="";
                         /*$akce1='<form name="UdalostUpravit" method="POST" action="UdalostUpravit.php">'
                                . sprintf('<input type="hidden" name="udalost_id" value="%s"/>', $row['id'])
                                . '<input type="submit" value="Upravit" class="EventListActionStyle"/>'
                                . '</form>';*/
                         } else $akce1="";
                        }
                        else { // jsem jen host
                          printf('<td title="Pozval mne %s, %s"><em>%s*</em></td>', 
                                  PersonName($_SESSION['Global']['People'][$row['person']]), 
                                  $PopisTypuUdalostiDetail[$row['type']],
                                  $PopisTypuUdalosti[$row['type']]);
                          $akce0 = '<form name="UdalostOdhlasit" method="POST" action="vogoni.php">'
                                . '<input type="hidden" name="TypDat" value="UdalostOdhlasit">'
                                . sprintf('<input type="hidden" name="smazat_udalost_id" value="%s"/>', $row['id'])
                                . '<input type="image" value="Odhlásit" class="EventListActionStyle" src="img/user-delete-icon.png" alt="Odhlásit" title="Odhlásit"/>'
                                . '</form>';  
                          $akce1="";
                          }
                            
                        if ($row['type']==3) {
                            printf('<td title="%s">%s</td><td title="%s">%s</td>', 
                                    date('D d. m. Y, G:i', strtotime($row['DateBegin'])), 
                                    date('d. m., G:i', strtotime($row['DateBegin'])), 
                                    date('D d. m. Y, G:i', strtotime($row['DateEnd'])),
                                    date('G:i', strtotime($row['DateEnd'])));
                        } else {
                            printf('<td title="%s">%s</td><td title="%s">%s</td>', 
                                    date('D d. m. Y', strtotime($row['DateBegin'])), 
                                    date('d. m.', strtotime($row['DateBegin'])), 
                                    date('D d. m. Y', strtotime($row['DateEnd'])),
                                    date('d. m.', strtotime($row['DateEnd'])));
                        }    
                        printf('<td>%s %s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>', 
                                $row['DestinationCity'],$row['DestinationCountry'], Granty($row['id']), $akce0, $akce1, $akceTisk[$row['type']], $row['Reason'], $row['Remark']);
                        
                        
                        if (AmIAdmin(2)) {
                            //echo '<td>';print_r($row);echo'</td>';

                            echo '<td><form name="UdalostGenerujPdf" method="POST" action="vogoni.php">';
                                echo '<input type="hidden" name="TypDat" value="UdalostGenerujPdf"/>';
                                printf('<input type="hidden" name="generuj_pdf_id" value="%s"/>', $row['id']);
                                echo '<input type="submit" value="pdf!"" alt="pdf!" title="Generuj Pdf"/>';
                            echo '</form></td>';
                            
                            printf('<td><small>%d</small></td>', $row['id']);
                        }
                        echo '</tr>';
                    }
                    mysql_free_result($udalosti)
                    ?>
                </tbody>
            </table><td></tr>

<!--    <tr><td><table class="EventListTableStyle">
        <thead>
            <th> Ladíme to. Tady si lze prohlédnout data v tabulkách:</th>
        </thead>
        <tbody>
            <tr>
                <td><a href="phpmyedit/people.php" target="_blank">people</a></td>
            </tr>
            <tr>
                <td><a href="phpmyedit/PeopleDetails.php" target="_blank">PeopleDetails</a></td>
            </tr>
            <tr>
                <td><a href="phpmyedit/SimpleEvents.php" target="_blank">SimpleEvents</a></td>
            </tr>
        </tbody></table></td></tr>-->
</table>